# -*- coding: utf-8 -*-
# Copyright (C) 2007 Boris Dušek
# See the file LICENCE in the top-level directory of this distribution
# for licensing information.
# vim:ts=4:sw=4:et

from army.modules.memory import FileMemoryEngine
import unittest

import StringIO

class FileMemoryEngineTest(unittest.TestCase):

    def testRead(self):
        buf = StringIO.StringIO('\x01\x02\x03\x04\x06\x07\x08\x09')
        mem = FileMemoryEngine.FileMemoryEngine(buf)
        self.assertEqual(mem.read(3), '\x04')
        self.assertEqual(mem.read_many(2,3), '\x03\x04\x06')

    def testWrite(self):
        buf = StringIO.StringIO()
        mem = FileMemoryEngine.FileMemoryEngine(buf)
        mem.write(6,'\x08')
        mem.write_many(2,'\x02\x03\x12')
        mem.write(3,'\x33')
        mem.write_many(4,'\x44\x55')
        self.assertEqual(buf.getvalue(), '\x00\x00\x02\x33\x44\x55\x08')

    def testReadWrite(self):
        buf = StringIO.StringIO()
        mem = FileMemoryEngine.FileMemoryEngine(buf)
        mem.write_many(3,'\x34\x45\x83\x42\x12')
        self.assertEqual(mem.read_many(5,2),'\x83\x42')

if __name__ == '__main__':
    unittest.main()
